import { onMounted, onUnmounted, onActivated, onDeactivated } from "vue";

export const onScrollToBottom = (
  handler: () => void,
  when: "onMounted" | "onActivated" = "onMounted"
) => {
  const onScrolling = () => {
    if (window.innerHeight + window.scrollY >= document.body.scrollHeight)
      handler();
  };
  const [on, off] =
    when == "onMounted"
      ? [onMounted, onUnmounted]
      : [onActivated, onDeactivated];
  on(() => {
    window.addEventListener("scroll", onScrolling);
  });
  off(() => {
    window.removeEventListener("scroll", onScrolling);
  });
};
